/*
Copyright (C) 2014 Yuvraj Singh Babrah

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


package design;

import java.awt.Color;
import java.awt.Dimension;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Yuvraj Singh Babrah
 */
public class DownloadsFrame extends javax.swing.JFrame {
    /* props */
    public static List downloads = new LinkedList();
    public static JPanel DownloadsPanelStatic = null;
    public static JLabel DownloadProgressStatic = null;

    /**
     * Creates new form DownloadsFrame
     */
    public DownloadsFrame() {
        initComponents();
        setIcon();
        customChanges();
    }
    
    /**
     * setIcon
     */
    public void setIcon() {
        try{
            URL iconURL = getClass().getResource("/resources/icon_32.png");
            ImageIcon icon = new ImageIcon(iconURL);
            this.setIconImage(icon.getImage());
        } catch(Exception e) { }
    }
    
    /**
     * customChanges
     */
    public void customChanges() {
        this.getContentPane().setBackground(new Color(102, 102, 102));
        this.setLocationRelativeTo(null);
        DownloadsPanel.setLayout(new BoxLayout(DownloadsPanel, BoxLayout.Y_AXIS));
        
        this.DownloadsPanelStatic = DownloadsPanel;
        this.DownloadProgressStatic = downloadProgress;
        
        this.setSize(this.getWidth(), 720);
        this.DownloadsPanel.setPreferredSize(new Dimension(991, 100));
    }
    
    /**
     * addPanel
     */
    public static void addPanel(JPanel panel) {
        /* add panel to the frame */
        DownloadsPanelStatic.add(panel);
        DownloadsPanelStatic.validate();
        /* add panel in the list */
        downloads.add(panel);
        DownloadProgressStatic.setVisible(false);
    }
    
    /**
     * removePanel
     * @param panel 
     */
    public static void removePanel(JPanel panel, int total, String albumName) {
        /* remove panel from the frame */
        DownloadsPanelStatic.remove(panel);
        DownloadsPanelStatic.validate();
        DownloadsPanelStatic.repaint();
        /* remove the panel from the list */
        downloads.remove(panel);
        if(downloads.size() <= 0) {
            DownloadProgressStatic.setVisible(true);
            JOptionPane.showMessageDialog(null, total + " photos from " + albumName + "downloaded, successfully!", "Download Complete!", JOptionPane.INFORMATION_MESSAGE, null);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        DownloadsScroll = new javax.swing.JScrollPane();
        DownloadsPanel = new javax.swing.JPanel();
        downloadProgress = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem1 = new javax.swing.JMenuItem();

        setTitle("My Downloads");
        setResizable(false);

        DownloadsScroll.setBackground(new java.awt.Color(255, 255, 255));
        DownloadsScroll.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        DownloadsScroll.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        DownloadsPanel.setBackground(new java.awt.Color(255, 255, 255));

        downloadProgress.setBackground(new java.awt.Color(255, 255, 255));
        downloadProgress.setFont(new java.awt.Font("Segoe UI", 1, 11)); // NOI18N
        downloadProgress.setForeground(new java.awt.Color(153, 153, 153));
        downloadProgress.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        downloadProgress.setText("No Downloads in Progress");
        downloadProgress.setBorder(javax.swing.BorderFactory.createMatteBorder(10, 10, 10, 10, new java.awt.Color(255, 255, 255)));
        downloadProgress.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        downloadProgress.setOpaque(true);
        downloadProgress.setPreferredSize(new java.awt.Dimension(940, 15));

        javax.swing.GroupLayout DownloadsPanelLayout = new javax.swing.GroupLayout(DownloadsPanel);
        DownloadsPanel.setLayout(DownloadsPanelLayout);
        DownloadsPanelLayout.setHorizontalGroup(
            DownloadsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(DownloadsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(downloadProgress, javax.swing.GroupLayout.PREFERRED_SIZE, 940, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(116, Short.MAX_VALUE))
        );
        DownloadsPanelLayout.setVerticalGroup(
            DownloadsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(DownloadsPanelLayout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(downloadProgress, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(596, Short.MAX_VALUE))
        );

        DownloadsScroll.setViewportView(DownloadsPanel);

        jMenu1.setText("Downloads");

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_J, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Download Destination");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);
        jMenu1.add(jSeparator1);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Close");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(DownloadsScroll)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(DownloadsScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO dispose the frame
        this.setVisible(false);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO show file chooser
        new DirectoryChooser();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel DownloadsPanel;
    private javax.swing.JScrollPane DownloadsScroll;
    private javax.swing.JLabel downloadProgress;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    // End of variables declaration//GEN-END:variables
}
